﻿using BaseData.CommonLib.Constants;
using BaseData.CommonLib.Models;
using Newtonsoft.Json;
using Swimj.CommonLib.Base;
using Swimj.Core.Extensions;
using System.Net.Http.Json;

namespace BaseData.RazorLib.Services
{
    public class MaterialCategoryService : IMaterialCategoryService
    {
        private readonly IHttpClientFactory _httpClientFactory;

        public MaterialCategoryService(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }

        public async Task<Result<List<MaterialCategoryTreeResponseDto>>?> QueryMaterialCategoriesByTree()
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response = await httpClient.GetAsync($"/{ApiRoutes.MaterialCategory.TreeMaterialCategory}");
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<List<MaterialCategoryTreeResponseDto>>>(resultJson);
                    }
                }

                return new Result<List<MaterialCategoryTreeResponseDto>>();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<PaginatedResult<MaterialCategoryInfoResponseDto>?> QueryMaterialCategories(
            MaterialCategorySearchRequestDto searchRequestDto)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response = await httpClient.GetAsync(
                    searchRequestDto.ToQueryString(ApiRoutes.MaterialCategory.QueryMaterialCategories));
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<PaginatedResult<MaterialCategoryInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<MaterialCategoryInfoResponseDto>?> GetMaterialCategory(int id)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response =
                    await httpClient.GetAsync(
                        $"/{ApiRoutes.MaterialCategory.GetMaterialCategory.Replace("{id}", id.ToString())}");
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<MaterialCategoryInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<MaterialCategoryInfoResponseDto>?> CreateMaterialCategory(
            MaterialCategoryEditRequestDto model)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response =
                    await httpClient.PostAsJsonAsync($"/{ApiRoutes.MaterialCategory.CreateMaterialCategory}", model);
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<MaterialCategoryInfoResponseDto>>(resultJson);
                    }
                }

                return new Result<MaterialCategoryInfoResponseDto>();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<MaterialCategoryInfoResponseDto>?> UpdateMaterialCategory(int id,
            MaterialCategoryEditRequestDto model)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response = await httpClient.PutAsJsonAsync(
                    $"/{ApiRoutes.MaterialCategory.UpdateMaterialCategory.Replace("{id}", id.ToString())}", model);
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<MaterialCategoryInfoResponseDto>>(resultJson);
                    }
                }

                return new Result<MaterialCategoryInfoResponseDto>();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }
}